#include "RootNode.h"

namespace Rz { namespace Graphics { namespace Scene {

RootNode::RootNode()
	: Node(NodeType::Root)
{

}

void RootNode::Add(Node* node)
{
	Node::Add(node);
	this->AddToMeshList(node);
}

void RootNode::AddToMeshList(Node* node)
{
	switch (node->GetNodeType())
	{
		case NodeType::Camera:
			if (_activeCamera == nullptr)
			{
				_activeCamera = node->As<CameraNode*>();
			}

			_cameras.Add(node->As<CameraNode*>());
			break;

		case NodeType::Mesh:	_meshes. Add(node->As<MeshNode*>());			break;
		case NodeType::Light:	_lights. Add(node->As<Lights::LightNode*>());	break;
		default: break;
	}

	if (node->GetChildrenCount())
	{
		size_t count = node->GetChildrenCount();

		while (count--)
		{
			this->AddToMeshList(node->GetChild(count));
		}
	}
}

void RootNode::RebuildTree()
{
	_cameras.Clear();
	_lights.Clear();
	_meshes.Clear();

	for (auto& e : this->GetChildren())
	{
		this->AddToMeshList(e);
	}
}

} } }
